package com.gitee.loyo.interfaces

import com.gitee.loyo.DataClass
import com.gitee.loyo.SimpleClass
import com.gitee.loyo.TUser
import com.gitee.loyo.act.User
import com.gitee.loyo.interfaces.Reflection.DEFAULT.getFieldType
import com.gitee.loyo.interfaces.Reflection.DEFAULT.getFieldValue
import com.gitee.loyo.interfaces.Reflection.DEFAULT.newDataInstance
import com.gitee.loyo.interfaces.Reflection.DEFAULT.overrideProperties
import org.junit.Assert.*
import org.junit.Test


class ReflectionTest {

    @Test
    fun testNewInstance(){
        assertNotNull(Reflection.DEFAULT.newInstance(SimpleClass::class))
    }

    @Test
    fun testNewDataInstance(){
        assertNotNull(newDataInstance(DataClass::class, mapOf<String, Any?>(
            "c" to false
        )))
    }

    @Test
    fun testGetFieldType(){
        assertEquals(getFieldType(SimpleClass::class, listOf("a")), String::class)
        assertNotEquals(getFieldType(SimpleClass::class, listOf("a")), Int::class)
        assertEquals(getFieldType(SimpleClass::class, listOf("b")), Int::class)
        assertEquals(getFieldType(SimpleClass::class, listOf("c")), Boolean::class)
    }

    @Test
    fun testGetFieldValue(){
        assertEquals(getFieldValue(Child(Parent(Grand())), "parent", "parent", "name"), "grand")
    }


    @Test
    fun testOverrideProperties(){
        val user1 = User(1L, null, 1)
        val user2 = User(2L, null, null)
        overrideProperties(user1, user2)
        assertEquals(user1, user2)
    }

    data class Grand(
        val name: String = "grand"
    )

    data class Parent(
        val parent: Grand
    )

    data class Child(
        val parent: Parent
    )
}